package tij4.concurrent.exercise.ex11;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Practice01Checker implements Runnable{

    private Practice01 practice01;
    private final int id;

    public Practice01Checker(Practice01 practice01, int id) {
        this.practice01 = practice01;
        this.id = id;
    }

    @Override
    public void run() {
        System.out.println("Testing...");
        while (!practice01.isCanceled()) {
            int[] range = practice01.next();
            if(range[0] > range[1]) {
                System.out.println("Error in test #" + id + ": min" + range[0]
                    + " > " + "max " + range[1]);
                practice01.cancel();
            }
        }
    }

    public static void test(Practice01 gen, int count){
        System.out.println("Press ctrl+c to exit!");
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < count; i++) {
            executorService.execute(new Practice01Checker(gen, i));
        }
    }

    public static void test(Practice01 gen) {
        test(gen, 10);
    }
}
